package xkp.lesson.producer.controller;

import com.alibaba.fastjson.JSON;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xkp.lesson.producer.entity.MQMessage;
import xkp.lesson.producer.entity.StudentInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 *
 */
@RestController
public class ProducerController {

    @Resource
    private MessageChannel output; //消息发送管道
    @Autowired
    private HttpServletRequest request;

    @PostMapping("/test-stream")
    public boolean testStream(@RequestBody StudentInfo studentInfo){
        //String serial = UUID.randomUUID().toString();
        MQMessage<StudentInfo> msg = new MQMessage<>();
        msg.setData(studentInfo);
        String token = request.getHeader("token");
        msg.setToken(token);
        String msgStr = JSON.toJSONString(msg);
        return output.send(MessageBuilder.withPayload(msgStr).build());
    }
}
